<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- 全选框 -->
        <label for="allChecked">全选</label>
        <input type="checkbox" id="allChecked" v-model="isAllSelect" @click="Allcheck(isAllSelect)">
        <!-- 购物车列表 -->
        <ul>
            <li v-for="(item,index) in dataArr">
                <input type="checkbox" v-model="item.checked" @change="ifAllCheck(item.checked)">
                商品名称: <span>{{item.name}}</span> |
                商品单价: <span>{{item.price}}</span> |
                数量: <input type="number" min="1" v-model="item.num"> |
                小计: <span>¥ {{item.price*item.num}}</span>
            </li>
        </ul>

        <span>总价:¥ {{allSum}}</span>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                isAllSelect: false,
                //flag控制是否全选
                flag:0,
                dataArr: [{
                    id: 1,
                    name: "iPhone14",
                    price: "5999",
                    num: 1,
                    checked: false
                },
                {
                    id: 2,
                    name: "iPhone14 Pro",
                    price: "7999",
                    num: 1,
                    checked: false
                },
                {
                    id: 3,
                    name: "iPhone 14 Pro Max",
                    price: "8999",
                    num: 1,
                    checked: false
                }
                ],
       

            },
            computed: {
                allSum() {
                    let sum=0
                    this.dataArr.forEach((item)=>{
                        if (item.checked) {
                            sum+=item.num*item.price; 
                        }
                    })
                    return sum;
                },
             
            },
            methods: {
         

                Allcheck(flag) {
                    //flag反回的接果慢一拍，就我点勾了返回false，取消点勾返回true
                    //点勾
                    (!flag)&&(this.flag=3);
                    //取消点勾
                    (flag)&&(this.flag=0);
                    //判断有没有全点勾
                    this.dataArr.forEach((item, index) => {
                        for (key in item) {
                            item.checked = !flag;
                        }
                    })
                   

                },
                ifAllCheck(flag){
                    console.log(this.flag);
                    if(flag){
                      this.flag++
                      //当全this.flag==3就是全点勾了
                        if(this.flag==3){
                            this.isAllSelect=true;
                            console.log( this.isAllSelect);
                        }
                    }else{
                        this.flag--;
                        //当全this.flag!=3就是没全点勾了
                        if(this.flag!=3){
                            this.isAllSelect=false;
                            console.log( this.flag);
                        }
                    }
                 
                }
                
            }
        })
    </script>
</body>

</html>